package com.crm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.annotation.Log;
import com.crm.base.BusinessType;
import com.crm.base.OperatorType;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.QueryOperateLog;
import com.crm.domain.entity.OperateLog;
import com.crm.service.OperateLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Xanny
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/monitor/operlog")
public class OperateLogController {
    private final OperateLogService operateLogService;

    /**
     * 分页查询
     * @param queryOperateLog
     * @return
     */
    @GetMapping("/list")
    @Log(title = "操作日志",businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
    public ResponseResult queryOperateLog(QueryOperateLog queryOperateLog) {
        Page<OperateLog> Logs = operateLogService.queryAll(queryOperateLog);
        return ResponseResult.success()
                .put("rows", Logs.getRecords())
                .put("total", Logs.getTotal());
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    @Log(title = "操作日志",businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
    public ResponseResult deleteByIds(@PathVariable List<Long> ids) {
        ids.forEach(id -> {
            operateLogService.lambdaUpdate().eq(OperateLog::getOperId,id).remove();
        });
        return ResponseResult.success();
    }

    /**
     * 清除所有
     * @return
     */
    @DeleteMapping("/clean")
    @Log(title = "操作日志",businessType = BusinessType.CLEAN,operatorType = OperatorType.MANAGE)
    public ResponseResult deleteAllNotice() {
        List<OperateLog> operateLogList = operateLogService.lambdaQuery().list();
        operateLogList.forEach(operateLog -> {
            operateLogService.lambdaUpdate().eq(OperateLog::getOperId,operateLog.getOperId()).remove();
        });
        return ResponseResult.success();
    }
}
